//
//  ViewController.m
//  19-朱大哥json
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
#import "YMJTableViewCell.h"
#import "Brandlist.h"
#import "List.h"
#import "YMJCollectionViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *countryArr;
@property (nonatomic, strong) NSMutableArray *brandlistArr;
@property (nonatomic, strong) NSMutableArray *hotTypeArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *coll;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 数据1
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AllCar" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *brandlistArr = dataDic[@"result"][@"brandlist"];
    _brandlistArr = [NSMutableArray array];
    
    for (NSDictionary *dic in brandlistArr) {
        Brandlist *br = [Brandlist initWithDic:dic];
        [_brandlistArr addObject:br];
       
    }
    // 数据2
    _hotTypeArr = [NSMutableArray array];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"HotBrand" ofType:@"json"];
    NSData *data1 = [NSData dataWithContentsOfFile:path1];
    NSDictionary *dataDic1 = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
    for (NSDictionary *dic in dataDic1[@"result"][@"list"]) {
        List *li = [List initWithDic:dic];
        [_hotTypeArr addObject:li];
    }
    // 数据三
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"YMJTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"1"];
    self.tableView.rowHeight = 70;
    // 透视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 414 / 5 * 2)];
    self.tableView.tableHeaderView = view;
    view.backgroundColor = [UIColor redColor];
    // collect
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(414 / 5, 414 / 5);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.coll = [[UICollectionView alloc] initWithFrame:view.bounds collectionViewLayout:layout];
    [view addSubview:_coll];
    _coll.delegate = self;
    _coll.dataSource = self;
    [_coll registerNib:[UINib nibWithNibName:@"YMJCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"1"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _hotTypeArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YMJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
    List *li = _hotTypeArr[indexPath.row];
    cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:li.img]]];
    cell.label.text = li.name;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[_brandlistArr[section] list] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _brandlistArr.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_brandlistArr[section] letter];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YMJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
    NSArray *arr = [_brandlistArr[indexPath.section] list];
    cell.label.text = arr[indexPath.row][@"name"];
    NSURL *url = [NSURL URLWithString:arr[indexPath.row] [@"imgurl"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    cell.imageView.image = [UIImage imageWithData:data];
    cell.label1.text = [arr[indexPath.row][@"id"] description] ;
//    NSLog(@"%@",arr[indexPath.row][@"id"]);
    return cell;
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView  {
    NSMutableArray *arr = [NSMutableArray array];
    for (Brandlist *br in _brandlistArr) {
        [arr addObject:br.letter];
    }
    return [NSArray arrayWithArray:arr];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return index;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
